import os
import sys
from dataclasses import dataclass

@dataclass
class Config:
    # 应用程序标识
    APP_NAME: str = '运维工作台通知助手'
    APP_ID: str = 'WS.Notifier.1'  # Windows 应用程序 ID
    
    # WebSocket 配置
    WS_URL: str = os.getenv('WS_URL', 'ws://172.18.1.102/ws')
    
    # 重连配置
    RECONNECT_DELAY: int = 5  # 重连延迟（秒）
    MAX_RECONNECT_ATTEMPTS: int = -1  # -1 表示无限重试
    
    # 日志配置
    LOG_LEVEL: str = os.getenv('LOG_LEVEL', 'INFO')
    LOG_FILE: str = os.getenv('LOG_FILE', 'ws_notifier.log')
    
    # 通知配置
    NOTIFICATION_TIMEOUT: int = int(os.getenv('NOTIFICATION_TIMEOUT', '10'))
    NOTIFICATION_SOUND: bool = os.getenv('NOTIFICATION_SOUND', 'true').lower() == 'true'
    NOTIFICATION_ICON: str = os.getenv('NOTIFICATION_ICON', '')  # 自定义通知图标
    
    # GUI配置
    TRAY_ICON_PATH: str = os.getenv(
        'TRAY_ICON_PATH',
        os.path.abspath(os.path.join(
            os.path.dirname(__file__),
            'gui',
            'assets',
            'icon.png'
        ))
    )
    
    # 配置文件路径
    APP_DATA_DIR: str = os.path.join(os.path.expanduser('~'), '.ws_notifier')
    CONFIG_PATH: str = os.path.join(APP_DATA_DIR, 'config.json')
    ACCOUNT_PATH: str = os.path.join(APP_DATA_DIR, 'account.json')

    # 用户信息 API 配置
    USER_INFO_API_URL: str = os.getenv('USER_INFO_API_URL', 'http://172.18.1.102/api/users/basic?userid=')

    def __post_init__(self):
        self._validate_config()
        # 确保图标目录存在
        icon_dir = os.path.dirname(self.TRAY_ICON_PATH)
        if not os.path.exists(icon_dir):
            os.makedirs(icon_dir, exist_ok=True)

    def _validate_config(self):
        """验证配置有效性"""
        if not self.WS_URL.startswith(('ws://', 'wss://')):
            raise ValueError("Invalid WebSocket URL")
        
        if self.RECONNECT_DELAY < 1:
            raise ValueError("RECONNECT_DELAY must be positive")
    